package com.lxyk.utils;

import java.util.*;

public class StatisticUtils {
    //获取开始时间和结束时间所有月份
    public static HashMap<Integer,List<String>> getMonths(Date startDate,Date endDate){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        // 获取开始年份和开始月份
        int startYear = calendar.get(Calendar.YEAR);
        int startMonth = calendar.get(Calendar.MONTH);
        // 获取结束年份和结束月份
        calendar.setTime(endDate);
        int endYear = calendar.get(Calendar.YEAR);
        int endMonth = calendar.get(Calendar.MONTH);
        //创建集合,保存所有月份
        HashMap<Integer,List<String>> map = new HashMap<>();//key:年  value:月份

        for (int i = startYear; i <= endYear; i++) {
            List<String> list = new ArrayList<String>();
            String month = "";
            if (startYear == endYear) {
                for (int j = startMonth; j <= endMonth; j++) {
                    if (j < 9) {
                        month = "0" + (j + 1);
                    } else {
                        month =  (j + 1)+"";
                    }
                    list.add(month);
                }
            } else {
                if (i == startYear) {
                    for (int j = startMonth; j < 12; j++) {
                        if (j < 9) {
                            month = "0" + (j + 1);
                        } else {
                            month =  (j + 1)+"";
                        }
                        list.add(month);
                    }
                } else if (i == endYear) {
                    for (int j = 0; j <= endMonth; j++) {
                        if (j < 9) {
                            month = "0" + (j + 1);
                        } else {
                            month =  (j + 1)+"";
                        }
                        list.add(month);
                    }
                } else {
                    for (int j = 0; j < 12; j++) {
                        if (j < 9) {
                            month = "0" + (j + 1);
                        } else {
                            month =  (j + 1)+"";
                        }
                        list.add(month);
                    }
                }
            }
            map.put(i,list);
        }

        return map;
    }

    //根据Date获取年月
    public static Integer[] getYearAndMonth(Date date){
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        Integer year = instance.get(Calendar.YEAR);
        Integer month = instance.get(Calendar.MONTH)+1;
        int day = instance.get(Calendar.DAY_OF_MONTH);
        Integer[] data = {year,month,day};
        return data;
    }
}
